package capstone.tagit;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

/**
 * 
 * The activity for creating a tag.  Includes options to specify all the attributes,
 * including name, description, and whatever configurable attributes are included.
 * Also includes the option for specifying tag type.
 * TaggingActivity is created with the requestcode 1 for tagging at current location, 2 for a specified location
 * Sends back resultcode 0 if tagging is cancelled, 1 if a tag has been submitted.
 * This activity will not be created at current location if GPS is currently off.
 *
 */
public class TaggingActivity extends Activity {

	/**
	 * A menuSettings object that contains information shared between Activities.  For this Activity, the field
	 * of interest is COLOR_CHOICE
	 */
	SharedPreferences menuSettings;
	/**
	 * An Editor to make changes to menuSettings.
	 */
	SharedPreferences.Editor prefEditor;
	/**
	 * A list of Strings corresponding to the Attibute fields.
	 */
	ArrayList<String> attributes;
	/**
	 * A list of TagType names available for the app.
	 */
	ArrayList<String> tagTypes;
	/**
	 * A list of the text boxes where the user types in their Tag information.
	 */
	ArrayList<EditText> attributeFields;
	/**
	 * The layout acquired from XML.
	 */
	LinearLayout customAttributesLayout;
	
	/**
	 * Called at Activity instantiation.
	 * Sets up the user interface and the tagging and attribute lists.
	 */
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		
		menuSettings = getSharedPreferences("MenuPref", MODE_PRIVATE);
		prefEditor = menuSettings.edit();
		setContentView(R.layout.tagging);
		customAttributesLayout = (LinearLayout)findViewById(R.id.customAttributesLayout);
		Intent myIntent = getIntent();
		attributes = myIntent.getStringArrayListExtra("attributes");
		attributeFields = new ArrayList<EditText>();
		tagTypes = myIntent.getStringArrayListExtra("tagTypes");
		configureUI();
		configureSpinner();
		
		Button submitButton = (Button)findViewById(R.id.submitButton);
		submitButton.setOnClickListener(submitListener);
		Button cancelButton = (Button)findViewById(R.id.cancelButton);
		cancelButton.setOnClickListener(cancelListener);		
	}
	
	/**
	 * Configures the EditTexts on the User Interface.
	 */
	private void configureUI(){
		for (String attribute : attributes){
			TextView prompt = new TextView(this);
			prompt.setText(attribute);
			EditText input = new EditText(this);
			customAttributesLayout.addView(prompt);
			customAttributesLayout.addView(input);
			attributeFields.add(input);
		}
	}
	/**
	 * Configures the Color Selection Spinner.
	 */
	void configureSpinner(){
		Spinner spinner = (Spinner) findViewById(R.id.tagTypeSpinner);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,
				tagTypes);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		spinner.setSelection(menuSettings.getInt("COLOR_CHOICE", 0));
		spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
			public void onItemSelected(AdapterView<?> parent, View view, int pos, long id){
				prefEditor.putInt("COLOR_CHOICE",pos);
				prefEditor.commit();
			}
			public void onNothingSelected(AdapterView<?> parent){
				// do nothing
			}
		});
	}
	/**
	 * The Listener for the submit button.  Puts all the inputted tag information
	 * into an Intent, which will be read by the main Activity.  ResultCode will be 1 to
	 * indicate creation of a new tag.  Ends the Activity
	 */
	private OnClickListener submitListener = new OnClickListener(){
		public void onClick(View view){
			Intent intent = new Intent();
			EditText titleInput = (EditText)findViewById(R.id.inputTitle);
			EditText descrInput = (EditText)findViewById(R.id.inputDescription);
			intent.putExtra("title",titleInput.getText().toString());
			intent.putExtra("description",descrInput.getText().toString());
			
			for (int i = 0; i < attributes.size(); i++){
				intent.putExtra(attributes.get(i),attributeFields.get(i).getText().toString());
			}
			setResult(1,intent);
			finish();
			
		}
	};
	/**
	 * Ends the Activty with resultCode 0, indicating that no Tag should be created.
	 */
	private OnClickListener cancelListener = new OnClickListener(){
		public void onClick(View view){
			setResult(0, new Intent());
			finish();
			
		}
	};
}